	.module util.c
	.area text(rom, con, rel)
	.dbfile D:\01.Projects\JKElectronics\ATMEGA\CoreModule\sw\examples\basic\lib\util.c
	.dbfunc e delay_us _delay_us fV
;              i -> R20,R21
;             us -> R16,R17
	.even
_delay_us::
	st -y,R20
	st -y,R21
	.dbline -1
	.dbline 6
; 
; #include "hw_config.h"
; #include "util.h"
; 
; void delay_us(u16 us)		  
; {
	.dbline 8
; 	u16 i;
; 	for( i=0;i<us;i++)
	clr R20
	clr R21
	xjmp L7
L4:
	.dbline 9
; 		asm("nop"); 
	nop
L5:
	.dbline 8
	subi R20,255  ; offset = 1
	sbci R21,255
L7:
	.dbline 8
	cp R20,R16
	cpc R21,R17
	brlo L4
X0:
	.dbline -2
L3:
	.dbline 0 ; func end
	ld R21,y+
	ld R20,y+
	ret
	.dbsym r i 20 i
	.dbsym r us 16 i
	.dbend
	.dbfunc e _delay_ms __delay_ms fV
;              i -> R20,R21
;       millisec -> R16,R17
	.even
__delay_ms::
	st -y,R20
	st -y,R21
	.dbline -1
	.dbline 15
; }
; 
; #ifndef USE_AVR_STUDIO
; /* This delay is very rough - don't use it for precise delays */
; void _delay_ms(unsigned int millisec)
; {
	xjmp L10
L9:
	.dbline 19
; 	u16 i;
;     
; 	while (millisec--)
;  		for (i=0; i<625; i++) nop(); //asm volatile ("nop"::);
	clr R20
	clr R21
	xjmp L15
L12:
	.dbline 19
	nop
L13:
	.dbline 19
	subi R20,255  ; offset = 1
	sbci R21,255
L15:
	.dbline 19
	cpi R20,113
	ldi R30,2
	cpc R21,R30
	brlo L12
X1:
L10:
	.dbline 18
	movw R2,R16
	subi R16,1
	sbci R17,0
	tst R2
	brne L9
	tst R3
	brne L9
X2:
	.dbline 19
	.dbline -2
L8:
	.dbline 0 ; func end
	ld R21,y+
	ld R20,y+
	ret
	.dbsym r i 20 i
	.dbsym r millisec 16 i
	.dbend
	.dbfunc e crc8 _crc8 fc
;            crc -> R10
;              i -> R20
;            len -> R18
;            ptr -> R16,R17
	.even
_crc8::
	xcall push_xgset300C
	.dbline -1
	.dbline 24
; }
; #endif
; 
; byte crc8(byte *ptr, byte len) 
; { 
	.dbline 26
; 	unsigned char i; 
; 	unsigned char crc=0; 
	clr R10
	xjmp L18
L17:
	.dbline 29
; 
; 	while(len--!=0) 
; 	{ 
	.dbline 30
; 		for(i=1; i!=0; i*=2) 
	ldi R20,1
	xjmp L23
L20:
	.dbline 31
; 		{ 
	.dbline 32
; 			if((crc&1)!=0) 
	sbrs R10,0
	rjmp L24
X3:
	.dbline 33
; 			{
	.dbline 34
; 				crc/=2; crc^=0x8C;
	lsr R10
	.dbline 34
	ldi R24,140
	ldi R25,0
	mov R2,R10
	clr R3
	eor R2,R24
	eor R3,R25
	mov R10,R2
	.dbline 35
; 			} 
	xjmp L25
L24:
	.dbline 37
; 			else 
; 			{
	.dbline 38
; 				crc/=2; 
	lsr R10
	.dbline 39
; 			}
L25:
	.dbline 40
; 			if((*ptr&i)!=0) 
	movw R30,R16
	ldd R2,z+0
	and R2,R20
	breq L26
X4:
	.dbline 41
; 				crc^=0x8C; 
	ldi R24,140
	ldi R25,0
	mov R2,R10
	clr R3
	eor R2,R24
	eor R3,R25
	mov R10,R2
L26:
	.dbline 42
; 		} 
L21:
	.dbline 30
	ldi R24,2
	mul R24,R20
	mov R20,R0
L23:
	.dbline 30
	tst R20
	brne L20
X5:
	.dbline 43
; 		ptr++; 
	subi R16,255  ; offset = 1
	sbci R17,255
	.dbline 44
; 	} 
L18:
	.dbline 28
	mov R2,R18
	clr R3
	subi R18,1
	tst R2
	brne L17
X6:
	.dbline 45
; 	return(crc); 
	mov R16,R10
	.dbline -2
L16:
	.dbline 0 ; func end
	xjmp pop_xgset300C
	.dbsym r crc 10 c
	.dbsym r i 20 c
	.dbsym r len 18 c
	.dbsym r ptr 16 pc
	.dbend
	.dbfunc e bcd2ascii _bcd2ascii fV
;          ptasc -> R18,R19
;            BCD -> R16
	.even
_bcd2ascii::
	.dbline -1
	.dbline 56
; }
; 
; /*******************************************
; Function name: bcd2ascii
; Function: to convert the compressed BCD code ascii code
; Parameters: BCD - BCD to be converted code compression
; ptasc - converted pointer to an array of ASCII code
; Return Value: None
; ********************************************/
; void bcd2ascii(byte BCD,byte ptasc[])
; { 	
	.dbline 57
; 	ptasc[0]= (BCD>>4) | 0x30;	 // convert ten
	mov R24,R16
	swap R24
	andi R24,#0x0F
	ori R24,48
	movw R30,R18
	std z+0,R24
	.dbline 58
; 	ptasc[1] = BCD & 0x0F | 0x30;  // convert bits
	mov R24,R16
	andi R24,15
	ori R24,48
	std z+1,R24
	.dbline -2
L28:
	.dbline 0 ; func end
	ret
	.dbsym r ptasc 18 pc
	.dbsym r BCD 16 c
	.dbend
	.dbfunc e __ftoa ___ftoa fV
;              s -> R20,R21
;              i -> R14,R15
;         length -> R10
;            rem -> R12,R13
;       DecPoint -> y+16
;            buf -> R22,R23
;              f -> y+10
	.even
___ftoa::
	xcall push_arg4
	xcall push_xgsetF0FC
	ldd R22,y+14
	ldd R23,y+15
	.dbline -1
	.dbline 64
; }
; 
; 
; void __ftoa(float f, unsigned char *buf, char DecPoint)
; 
; {
	.dbline 66
;    unsigned int rem;
;    unsigned char *s,length=0;
	clr R10
	.dbline 69
;    int i;
; 
;    i = (int)((float)f*10);
	ldi R16,<L30
	ldi R17,>L30
	xcall lpm32
	movw R24,R28
	adiw R24,10
	st -y,R25
	st -y,R24
	xcall fpmule1
	xcall fpint
	movw R14,R16
	.dbline 71
; 
;    s = buf;
	movw R20,R22
	.dbline 72
;    if (i == 0)                //print 0.0 with null termination here
	cpi R16,0
	cpc R16,R17
	brne L31
X7:
	.dbline 73
;    {
	.dbline 74
;       *s++ = '0';
	ldi R24,48
	movw R30,R20
	st Z+,R24
	.dbline 75
;       *s++ = '.';
	ldi R24,46
	st Z+,R24
	.dbline 76
;       *s++ = '0'; 
	ldi R24,48
	st Z+,R24
	movw R20,R30
	.dbline 77
;       *s=0;                   //null terminate the string
	clr R2
	std z+0,R2
	.dbline 78
;    } 
	xjmp L32
L31:
	.dbline 80
;    else 
;    {   
	.dbline 81
;       if (i < 0) 
	movw R24,R14
	cpi R24,0
	ldi R30,0
	cpc R25,R30
	brge L36
X8:
	.dbline 82
;       {
	.dbline 83
;          *buf++ = '-';
	ldi R24,45
	movw R30,R22
	st Z+,R24
	movw R22,R30
	.dbline 84
;          s = buf;
	movw R20,R30
	.dbline 85
;          i = -i;
	movw R24,R14
	com R24
	com R25
	subi R24,0xFF
	sbci R25,0xFF
	movw R14,R24
	.dbline 86
;       }
	xjmp L36
L35:
	.dbline 90
;       //while-loop to "decode" the long integer to ASCII by append '0', string in reverse manner
;       //If it is an integer of 124 -> string = {'4', '2', '1'}
;       while (i) 
;       {
	.dbline 91
;          ++length;
	inc R10
	.dbline 92
;          rem = i % 10;
	ldi R18,10
	ldi R19,0
	movw R16,R14
	xcall mod16s
	movw R12,R16
	.dbline 93
;          *s++ = rem + '0';
	movw R24,R12
	adiw R24,48
	movw R30,R20
	st Z+,R24
	movw R20,R30
	.dbline 94
;          i /= 10;
	ldi R18,10
	ldi R19,0
	movw R16,R14
	xcall div16s
	movw R14,R16
	.dbline 95
;       }
L36:
	.dbline 89
	tst R14
	brne L35
	tst R15
	brne L35
X9:
	.dbline 97
;       //reverse the string in this for-loop, string became {'1', '2', '4'} after this for-loop
;       for(rem=0; ((unsigned char)rem)<length/2; rem++) 
	clr R12
	clr R13
	xjmp L41
L38:
	.dbline 98
;       {
	.dbline 99
;          *(buf+length) = *(buf+((unsigned char)rem));
	mov R30,R12
	clr R31
	add R30,R22
	adc R31,R23
	ldd R2,z+0
	mov R30,R10
	clr R31
	add R30,R22
	adc R31,R23
	std z+0,R2
	.dbline 100
;          *(buf+((unsigned char)rem)) = *(buf+(length-((unsigned char)rem)-1));
	mov R2,R12
	clr R3
	movw R4,R2
	mov R30,R10
	clr R31
	sub R30,R2
	sbc R31,R3
	sbiw R30,1
	add R30,R22
	adc R31,R23
	ldd R2,z+0
	movw R30,R4
	add R30,R22
	adc R31,R23
	std z+0,R2
	.dbline 101
;          *(buf+(length-((unsigned char)rem)-1)) = *(buf+length);
	mov R2,R4
	clr R3
	mov R30,R10
	clr R31
	sub R30,R2
	sbc R31,R3
	sbiw R30,1
	add R30,R22
	adc R31,R23
	mov R26,R10
	clr R27
	add R26,R22
	adc R27,R23
	ld R2,x
	std z+0,R2
	.dbline 102
;       }
L39:
	.dbline 97
	movw R24,R12
	adiw R24,1
	movw R12,R24
L41:
	.dbline 97
	mov R2,R10
	lsr R2
	cp R12,R2
	brsh X12
	xjmp L38
X12:
X10:
	.dbline 105
; 
;       /* Take care of the special case of 0.x if length ==1*/   
;       if(length==1) 
	mov R24,R10
	cpi R24,1
	brne L42
X11:
	.dbline 106
;       {
	.dbline 107
;          *(buf+2) = *buf;
	movw R30,R22
	ldd R2,z+0
	std z+2,R2
	.dbline 108
;          *buf = '0';
	ldi R24,48
	std z+0,R24
	.dbline 109
;          *(buf+1) = '.';
	ldi R24,46
	std z+1,R24
	.dbline 111
; 
;          *(s+2)=0;                //null terminate
	clr R2
	movw R30,R20
	std z+2,R2
	.dbline 112
;       } 
	xjmp L43
L42:
	.dbline 114
;       else 
;       {
	.dbline 115
;             *(buf+length) = *(buf+length-1); ///== cai nay MOT dec point ==///
	mov R2,R10
	clr R3
	add R2,R22
	adc R3,R23
	movw R4,R2
	movw R30,R2
	sbiw R30,1
	ldd R2,z+0
	movw R30,R4
	std z+0,R2
	.dbline 116
;             *(buf+length-1)='.';
	mov R30,R10
	clr R31
	add R30,R22
	adc R31,R23
	sbiw R30,1
	ldi R24,46
	std z+0,R24
	.dbline 117
;             *(s+1)=0;                   //null terminate
	clr R2
	movw R30,R20
	std z+1,R2
	.dbline 118
;       }
L43:
	.dbline 119
;    }
L32:
	.dbline -2
L29:
	.dbline 0 ; func end
	xcall pop_xgsetF0FC
	adiw R28,4
	ret
	.dbsym r s 20 pc
	.dbsym r i 14 I
	.dbsym r length 10 c
	.dbsym r rem 12 i
	.dbsym l DecPoint 16 c
	.dbsym r buf 22 pc
	.dbsym l f 10 D
	.dbend
	.area lit(rom, con, rel)
L30:
	.word 0x0,0x4120
; }
; 
; 
; 
